package org.lq.us.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.lq.us.commons.Page;
import org.lq.us.entity.Users;
import org.lq.us.query.UsersQuery;
import org.lq.us.service.UsersService;
import org.lq.us.service.impl.UsersServiceImpl;

import com.google.gson.Gson;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UsersServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    UsersService usersService = new UsersServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		switch (method) {
		case "findAll":
			findAll(request, response);
			break;
		case "getName":
			getName(request, response);
			break;
		case "save":
			save(request, response);
			break;
		case "update":
			update(request, response);
			break;
		case "del":
			del(request, response);
			break;
		default:
			break;
		}
	}
	
	
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		if(usersService.delete(Integer.valueOf(id))) {
			response.getWriter().write("ok");
		}else {
			response.getWriter().write("no");
		}
		
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		 Users user = new Users();
		 try {
			BeanUtils.populate(user, request.getParameterMap());
			if(usersService.update(user)) {
				response.getWriter().write("ok");
			}else {
				response.getWriter().write("no");
			}
			//response.sendRedirect("page/addUser.jsp");
			
			
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		
	}
	
	protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		 Users user = new Users();
		 try {
			BeanUtils.populate(user, request.getParameterMap());
			if(usersService.save(user)) {
				response.getWriter().write("ok");
			}else {
				response.getWriter().write("no");
			}
			//response.sendRedirect("page/addUser.jsp");
			
			
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		
	}
	
	protected void getName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		 String name = request.getParameter("name");
		 boolean bool = usersService.getByName(name);
		 if(bool) {
			 response.getWriter().write("no");
		 }
		
	}
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		String pageIndex  = request.getParameter("page");
		String pageSize = request.getParameter("limit");
		
		String gender = request.getParameter("sex");
		String uname = request.getParameter("uname");
		String rid = request.getParameter("rid");
		
		UsersQuery uq = new UsersQuery();
		
		uq.setGender(gender);
		uq.setName(uname);
		uq.setRid(rid);
		
		
		Page<Users> page = new Page<Users>(Integer.parseInt(pageIndex), Integer.valueOf(pageSize), usersService.getCountQuery(uq));
		
		usersService.findPageQuery(page, uq);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count",page.getTotalCount());
		map.put("data",page.getList());
		
		response.getWriter().write(new Gson().toJson(map));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
